// pages/month/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show1: false,
    AreaList: [],
    areaName: '',
    show2: false,
    MealList: [],
    MealDetail: {},
    num: ['', '', '', '', '', '', '', ''],
    numIndex: 0,
    areaId: '',
    licensePlateNumber: '',
    // 二维码部分
    showCode: false,
    codeInfo: {
      tollCollectorPaymentOrderId: 111,
      paymentQRCode: 'https//asdasdasdasd'
    },
    tollCollectorPaymentOrderId: ''
  },
  // 选择某街道
  onConfirm(e) {
    console.log(e)
    this.onClose()
    if (e.detail.value.areaId) {
      // 选择区域
      this.setData({
        "areaId": e.detail.value.areaId,
        areaName: e.detail.value.areaName,
        MealDetail: {}
      })
      this.getMonthlySetMealByAreaId(e.detail.value.areaId)
    } else if (e.detail.value.monthlySetMealId) {
      // 选择套餐
      this.setData({
        MealDetail: e.detail.value
      })
    }

  },
  // 输入框改变事件
  numInput(e) {
    const {
      indx
    } = e.currentTarget.dataset
    const {
      value
    } = e.detail
    this.setData({
      ['num[' + indx + ']']: value,
      numIndex: (indx * 1) + 1
    })
    // 输入最后一位数校验
    if (indx == 7) {
      this.setData({
        'licensePlateNumber': this.data.num.join('')
      })
    }
  },
  // 显示选择街道
  showPopup(e) {
    console.log(e)
    if (this.data.tollcollectorpaymentorderid) {
      return
    }
    const {
      index
    } = e.currentTarget.dataset
    if (index == 2 && this.data.areaName == '') {
      wx.showToast({
        title: '请先选择包月区域',
        icon: 'none'
      })
      return
    }
    this.setData({
      [`show${index}`]: true
    });
  },
  // 关闭弹窗
  onClose() {
    this.setData({
      show1: false,
      show2: false,
      show3: false
    });
  },
  // 查询包月区域
  getAreaList() {
    this.setData({
      AreaList: [{
        areaId: 1,
        areaName: '测试区域'
      }]
    })
  },
  // 查询包月套餐,当用户选择或更选区域后,调用该方法
  getMonthlySetMealByAreaId() {
    this.setData({
      MealList: [{
        monthlySetMealId: 1,
        monthlySetMeaName: '测试套餐',
        validityPeriodStartTime: '2023.09.01',
        validityPeriodEndTime: '2023.09.30',
        amount: 50
      }]
    })
  },
  // 返回首页
  onClickLeft() {
    wx.switchTab({
      url: '/pages/work/index',
    })
  },
  // 提交事件
  submit(e) {
    console.log(e.detail.value)
    if (this.data.tollcollectorpaymentorderid) {
      return
    }
    const {
      carOwnerName,
      phoneNumber,
      familiarityNumber1,
      familiarityNumber2
    } = e.detail.value
    if (carOwnerName == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (phoneNumber == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
    if (this.data.areaId == '') {
      wx.showToast({
        title: '请选择包月区域',
        icon: 'none'
      })
      return
    }
    if (!this.data.MealDetail.monthlySetMealId) {
      wx.showToast({
        title: '请选择包月套餐',
        icon: 'none'
      })
      return
    }
    if (this.data.MealDetail.licensePlateNumber == '') {
      wx.showToast({
        title: '请输入车牌号',
        icon: 'none'
      })
      return
    }
    let familiarityNumber = []
    if (familiarityNumber1) {
      familiarityNumber.push(familiarityNumber1)
    }
    if (familiarityNumber2) {
      familiarityNumber.push(familiarityNumber2)
    }
    let params = {
      monthlyOrderId: '',
      areaId: this.data.areaId,
      monthlySetMealId: this.data.MealDetail.monthlySetMealId,
      licensePlateNumber: this.data.licensePlateNumber,
      carOwnerName,
      phoneNumber,
      familiarityNumber: familiarityNumber.join(','),
      tollCollectorId: wx.getSystemInfoSync('info').tollCollectorId
    }
    this.setData({
      showCode: true,
      codeInfo: {
        tollCollectorPaymentOrderId: 111,
        paymentQRCode: 'https//asdasdasdasd'
      }
    })
  },
  // 刷新二维码
  getPayQRCodeByTollCollectorOrder() {
    if (this.data.codeInfo.paymentQRCode) {
      this.setData({
        'codeInfo.paymentQRCode': new Date().getTime()
      })
    } else {
      wx.showToast({
        title: '暂无二维码信息',
        icon: 'none'
      })
    }
  },
  // 查询停车费补交或包月支付结果
  getPayResultsByPaymentOrderId() {
    if (this.data.codeInfo.paymentQRCode) {
      wx.showModal({
        title: '支付成功',
        content: `订单已被支付,支付金额为${this.data.params.amount}元`,
        showCancel: false,
        complete: (res) => {
          wx.switchTab({
            url: './ispay/index?type=2',
          })
        }
      })

    } else {
      wx.showToast({
        title: '暂无二维码信息',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getAreaList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
        list: [{
            "pagePath": "/pages/month/index",
            "text": "包月缴费",
            "iconPath": "/images/tabbar/month.png",
            "selectedIconPath": "/images/tabbar/montha.png"
          },
          {
            "pagePath": "/pages/month/nopay/index",
            "text": "未支付订单",
            "iconPath": "/images/tabbar/nopay.png",
            "selectedIconPath": "/images/tabbar/nopaya.png"
          },
          {
            "pagePath": "/pages/month/ispay/index",
            "text": "已支付订单",
            "iconPath": "/images/tabbar/ispay.png",
            "selectedIconPath": "/images/tabbar/ispaya.png"
          }
        ]
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})